﻿#ifndef CPUSHBUTTON_H
#define CPUSHBUTTON_H

/**
* @brief
 * 1. 可设置文本,居中显示
 * 2. 可设置文本颜色
 * 3. 设置图标(图标位置)
 * 4. 添加信号函数
 * 5. 设置按钮弧度
 * 6. 设置按钮字体大小
 * 7. 设置边框宽以及边框颜色
 * 8. 鼠标按下抬起颜色设置
 * 9. 鼠标悬浮颜色设置
* @author    Wanglipeng
* @email     1594932770@qq.com
* @date      2021-05-15
*/

#include <QObject>
#include <QWidget>
#include <QResizeEvent>
#include <QIcon>

class CPushButton: public QWidget
{
    Q_OBJECT
    Q_PROPERTY(QString text READ getText WRITE setText)
    Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)
    Q_PROPERTY(QColor backgroundColor READ getBackgroundColor WRITE setBackgroundColor)
    Q_PROPERTY(QColor borderColor READ getBorderColor WRITE setBorderColor)
    Q_PROPERTY(qreal radius READ getRadius WRITE setRadius)
    Q_PROPERTY(bool showIcon READ getShowIcon WRITE setShowIcon)
    Q_PROPERTY(QIcon icon READ getIcon WRITE setIcon)
    Q_PROPERTY(QSize iconSize READ getIconSize WRITE setIconSize)
    Q_PROPERTY(bool changeCursor READ getChangeCursor WRITE setChangeCursor)
    Q_PROPERTY(bool checkable READ getCheckable WRITE setCheckable)
    Q_PROPERTY(bool isChekced READ getChecked WRITE setChecked)


public:
    enum AlignmentFlag
    {
        Left,
        Right
    };
    explicit CPushButton(QWidget *parent = 0);

    QSize sizeHint() const;
    QSize minimumSizeHint() const;

public:
    QString getText() const;
    QColor getTextColor() const;
    qreal getRadius() const;
    QColor getBorderColor() const;
    QColor getBackgroundColor() const;
    QIcon getIcon() const;
    QFont getTextfont() const;

    QSize getIconSize() const;
    bool getShowIcon() const;

    bool getChangeCursor() const;
    bool getCheckable() const;
    bool getChecked() const;

public slots:
    CPushButton& setText(const QString &sText);
    CPushButton& setTextColor(const QColor &TextColor);
    CPushButton& setRadius(const qreal &dbRadius);
    CPushButton& setBorderColor(const QColor& BorderColor);
    CPushButton& setBackgroundColor(const QColor &BackgroundColor);
    CPushButton& setIcon(const QIcon &sIcons, CPushButton::AlignmentFlag alignment = CPushButton::Left);
    CPushButton& setIconSize(QSize size);
    CPushButton& setTextfont(const QFont &Textfont);
    CPushButton& setPressedColor(const QColor &clickedColor);
    CPushButton& setHoverColor(const QColor& hoverColor);
    CPushButton& setShowIcon(bool bShowIcon);
    CPushButton& setTextMargin(int left, int top, int right, int bottom);
    CPushButton& setChangeCursor(bool bChangeCursor);
    CPushButton& setCheckable(bool able);
    CPushButton& setChecked(bool checked);

protected:
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent * e);
    void mouseDoubleClickEvent(QMouseEvent *e);
//    void resizeEvent(QResizeEvent *event);
    void leaveEvent(QEvent*);
    void enterEvent(QEvent *event);

private:
    void drawTextOrIcons(QPainter*);        // 绘制图标和文字

Q_SIGNALS:
    void clicked();
    void doubleclicked();
    void rightclicked();
    void checkStateChange(bool);

private:
    Q_DISABLE_COPY(CPushButton)
    QString m_sText;                // 文字
    QIcon m_sIcons;                 // 图标
    QSize m_iconSize;               // 图标的大小
    bool m_bShowIcon;               // 是否显示图标
    AlignmentFlag m_nAlignment;     // 文字对齐方式

    QFont m_Textfont;               // 按钮文字字体

    QColor m_BackgroundColor;       // 背景颜色
    QColor m_PressgroundColor;      // 按下时背景颜色
    QColor m_HoverColor;            // hover时背景颜色
    QColor m_TextColor;             // 文字颜色
    QColor m_BorderColor;           // 边框颜色

    qreal m_dbRadius;               // 圆角大小
//    int m_iWidth;                   //
//    int m_iHeight;                  //
    int m_iBorderWidth;             // 边框宽度
    int m_marginLeft;
    int m_marginTop;
    int m_marginRight;
    int m_marginBottom;
    bool m_bChangeCursor;            // hover时是否改变鼠标形状,默认改变

    bool m_bHover;
    bool m_bPressed;
    bool m_bCheckable;              // 是否可选中;
    bool m_bChecked;                // 是否被选中
};

#endif // CPUSHBUTTON_H
